package com.sx.gongzhong.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.model.UserInfoBean;
import com.sx.gongzhong.pest.VerifyCodePest;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.VerifyCodeView;
import com.sx.gongzhong.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xiaofei.library.datastorage.DataStorageFactory;
import xiaofei.library.datastorage.IDataStorage;

/**
 * 更换手机号
 * Created by admin on 2018/1/2.
 */

public class UpdatePhoneActivity extends BaseActivity implements VerifyCodeView {

    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.et_Phone)
    EditText et_Phone;
    @BindView(R.id.et_Message)
    EditText et_Message;
    @BindView(R.id.tv_GetCode)
    TextView tv_GetCode;
    private long time = 60*1000;
    private CountDownTimer timer;
    private UserInfoBean userBean;
    private IDataStorage dataStorage;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_update_phone);
        ButterKnife.bind(this);
        setTitleName("更换手机号");
        initViews();
    }

    private void initViews() {
        dataStorage = DataStorageFactory.getInstance(getApplicationContext(),
                DataStorageFactory.TYPE_DATABASE);
        if (dataStorage.contains(UserInfoBean.class,getPackageName())){
            userBean = dataStorage.load(UserInfoBean.class,getPackageName());
            tv_phone.setText(BaseUtil.isEmpty(userBean.mobilePhone)?
                    "当前手机号：无":"当前手机号："+userBean.mobilePhone);
        }
    }

    @OnClick({R.id.tv_GetCode, R.id.btn_Next})
    public void onClick(View view) {
        String phone=et_Phone.getText().toString().trim();
        if (phone.length()!=11){
            et_Phone.setError("请输入正确的手机号");
            return;
        }
        switch (view.getId()) {
            case R.id.tv_GetCode:
                new VerifyCodePest(this).getVerifyCode(et_Phone.getText().toString().trim(),3);
                break;
            case R.id.btn_Next:
                String code=et_Message.getText().toString().trim();
                if (BaseUtil.isEmpty(code)){
                    ToastUtils.showToast(this,"请输入验证码");
                   return;
                }
                new VerifyCodePest(this).updatePhone(phone,code);
                break;
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(UpdatePhoneActivity.this);
        ToastUtils.showToast(UpdatePhoneActivity.this,message);
    }



    @Override
    public void getVerifyCodeSuccess(Boolean result) {
        tv_GetCode.setClickable(false);
        time = 60*1000;
        startTime();
    }

    @Override
    public void checkVerifyCodeSuccess(String message) {
        userBean.mobilePhone=et_Phone.getText().toString().trim();
        dataStorage.storeOrUpdate(userBean,getPackageName());
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
        finish();
    }


    @Override
    protected void onDestroy() {
        if (timer!=null){
            timer.cancel();
        }
        super.onDestroy();
    }

    private void startTime() {
        timer=new CountDownTimer(time,998) {
            @Override
            public void onTick(long millisUntilFinished) {
                time-=1000;
                if(time>0){
                    tv_GetCode.setText(""+time/1000+"s");
                    tv_GetCode.setTextColor(Color.parseColor("#fa4040"));
                }
            }
            @Override
            public void onFinish() {
                tv_GetCode.setText("重新获取");
                tv_GetCode.setTextColor(Color.parseColor("#000000"));
                tv_GetCode.setClickable(true);
            }
        }.start();
    }
}
